package com.atguigu.atcrowdfunding.controller;


import java.util.HashMap;
import java.util.Map;

/**
 * @author xzt
 * @create 2021-02-26 15:57
 */
public class BaseController {

    //private Map<String,Object> resultMap;//不能使用成员变量，因为控制器对象是单例的，会出现线程并发问题

    private ThreadLocal<Map<String, Object>> datas = new ThreadLocal<>();

    public void start(){
        Map<String,Object> resultMap = new HashMap<>();
        datas.set(resultMap);
    }

    public Object end(){
        Map<String,Object> resultMap = datas.get();
        datas.remove();
        return resultMap;
    }

    public void success(boolean flag){
        Map<String,Object> resultMap = datas.get();
        resultMap.put("success",flag);
    }

    public void param(String key,Object val){
        Map<String,Object> resultMap = datas.get();
        resultMap.put(key,val);
    }

    public void error(String msg){
        Map<String,Object> resultMap = datas.get();
        resultMap.put("message",msg);
    }


}
